//-----------------------------------------------------------------------
// <copyright file="tblUserDAOBase.cs" company="Scb">
//     Copyright ? Scb. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

using System;
using System.Data;
using System.Data.Common;
using Scb.Framework;
using Scb.Framework.Data;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Robot.Entities;

namespace Robot.Data
{    
    public abstract class tblUserDAOBase
    {
        #region Common methods
        public virtual tblUser CreatetblUserFromReader(IDataReader reader)
        {
            tblUser item = new tblUser();
            try
            {
                if (UtilityDAO.CheckColumnData(reader,"Peoplewise")) item.Peoplewise = (string)reader["Peoplewise"];
                if (UtilityDAO.CheckColumnData(reader,"UserName")) item.UserName = (string)reader["UserName"];
                if (UtilityDAO.CheckColumnData(reader,"Email")) item.Email = (string)reader["Email"];
                if (UtilityDAO.CheckColumnData(reader,"LastLoginDate")) item.LastLoginDate = (DateTime)reader["LastLoginDate"];
                if (UtilityDAO.CheckColumnData(reader,"Role")) item.Role = (int)reader["Role"];
                if (UtilityDAO.CheckColumnData(reader,"Status")) item.Status = (int)reader["Status"];
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessCreatetblUserFromReaderException, ex);
            }
            return item;
        }
        #endregion
        
        #region CreatetblUser methods
            
        public virtual void CreatetblUser(tblUser tblUser)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("sptblUserInsert");
                
                database.AddInParameter(dbCommand, "@Peoplewise", DbType.AnsiString, tblUser.Peoplewise);
                database.AddInParameter(dbCommand, "@UserName", DbType.AnsiString, tblUser.UserName);
                database.AddInParameter(dbCommand, "@Email", DbType.AnsiString, tblUser.Email);
                database.AddInParameter(dbCommand, "@LastLoginDate", DbType.DateTime, tblUser.LastLoginDate);
                database.AddInParameter(dbCommand, "@Role", DbType.Int32, tblUser.Role);
                database.AddInParameter(dbCommand, "@Status", DbType.Int32, tblUser.Status);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommand);
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessCreatetblUserException, ex);
            }
        }

        #endregion

        #region UpdatetblUser methods
        
        public virtual void UpdatetblUser(tblUser tblUser)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("sptblUserUpdate");            
                
                database.AddInParameter(dbCommand, "@Peoplewise", DbType.AnsiString, tblUser.Peoplewise);
                database.AddInParameter(dbCommand, "@UserName", DbType.AnsiString, tblUser.UserName);
                database.AddInParameter(dbCommand, "@Email", DbType.AnsiString, tblUser.Email);
                database.AddInParameter(dbCommand, "@LastLoginDate", DbType.DateTime, tblUser.LastLoginDate);
                database.AddInParameter(dbCommand, "@Role", DbType.Int32, tblUser.Role);
                database.AddInParameter(dbCommand, "@Status", DbType.Int32, tblUser.Status);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommand);
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessUpdatetblUserException, ex);
            }
        }
        
        #endregion

        #region DeletetblUser methods
        public virtual void DeletetblUser(string peoplewise)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("sptblUserDelete");
                
                database.AddInParameter(dbCommand, "@Peoplewise", DbType.AnsiString, peoplewise);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommand);
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessDeletetblUserException, ex);
            }
        }

        #endregion

        #region GettblUser methods
        
        public virtual tblUser GettblUser(string peoplewise)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("sptblUserGet");
                
                database.AddInParameter(dbCommand, "@Peoplewise", DbType.AnsiString, peoplewise);
                
                tblUser tblUser =  null;
                using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommand))
                {
                    if (reader.Read())
                    {
                        tblUser = CreatetblUserFromReader(reader);
                        reader.Close();
                    }
                }
                return tblUser;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessGettblUserException, ex);
            }
        }
        
        #endregion

        #region GettblUserList methods
        public virtual tblUserCollection GettblUserList(tblUserColumns orderBy, OrderDirection orderDirection, int page, int pageSize, out int totalRecords)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("sptblUserGetList");
                
                database.AddInParameter(dbCommand, "@OrderBy", DbType.AnsiString, orderBy.ToString());
                database.AddInParameter(dbCommand, "@OrderDirection", DbType.AnsiString, orderDirection.ToString());
                database.AddInParameter(dbCommand, "@Page", DbType.Int32, page);
                database.AddInParameter(dbCommand, "@PageSize", DbType.Int32, pageSize);
                database.AddOutParameter(dbCommand, "@TotalRecords", DbType.Int32, 4);
                
                tblUserCollection tblUserCollection = new tblUserCollection();
                using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommand))
                {
                    while (reader.Read())
                    {
                        tblUser tblUser = CreatetblUserFromReader(reader);
                        tblUserCollection.Add(tblUser);
                    }
                    reader.Close();
                }
                totalRecords = (int)database.GetParameterValue(dbCommand, "@TotalRecords");
                return tblUserCollection;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessGettblUserListException, ex);
            }
        }
        
        public virtual tblUserCollection GettblUserList(tblUserColumns orderBy, OrderDirection orderDirection)
        {            
            int totalRecords = 0;
            return GettblUserList(orderBy, orderDirection, 0, 0, out totalRecords);
        }
        
        #endregion
    }
}